package com.spic.business.train.trainInfo.controller;

import com.spic.business.train.trainInfo.domain.TrainInfo;
import com.spic.business.train.trainInfo.dto.TrainInfoAPPDto;
import com.spic.business.train.trainInfo.dto.TrainInfoAPPVo;
import com.spic.business.train.trainInfo.dto.TrainInfoDto;
import com.spic.business.train.trainInfo.dto.TrainListDto;
import com.spic.business.train.trainInfo.service.ITrainInfoService;
import com.spic.common.core.exception.ServiceException;
import com.spic.common.core.web.controller.BaseController;
import com.spic.common.core.web.domain.AjaxResult;
import com.spic.common.core.web.page.TableDataInfo;
import com.spic.common.log.annotation.Log;
import com.spic.common.log.enums.BusinessType;
import com.spic.common.security.annotation.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 培训信息Controller
 *
 * @author spic
 * @date 2021-10-15
 */
@RestController
@RequestMapping("/train/trainInfo")
public class TrainInfoController extends BaseController
{
    @Autowired
    private ITrainInfoService trainInfoService;

    /**
     * 查询培训信息列表
     */
    @PreAuthorize(hasPermi = "business/train:trainInfo:list")
    @PostMapping("/list")
    public TableDataInfo list(TrainListDto trainListDto)
    {
        startPage();
        List<TrainInfo> list = trainInfoService.selectTrainInfoList(trainListDto);
        return getDataTable(list);
    }

    @GetMapping("/getListByTrainInfoForApp")
    public TableDataInfo getListByTrainInfoForApp(TrainInfoAPPDto trainInfoAPPDto){
        startPage();
        List<TrainInfoAPPVo> list = trainInfoService.getListByTrainInfoForApp(trainInfoAPPDto);
        List<TrainInfoAPPVo> count = trainInfoService.getListByTrainInfoForAppCount(trainInfoAPPDto);
        TableDataInfo dataTable = getDataTable(list);
        if(count!=null && count.size()>0){
            dataTable.setTotal(count.size());
        }
        return dataTable;
    }
    /**
     * 查询培训信息列表
     */
    @PreAuthorize(hasPermi = "business/train:trainInfoPersonal:list")
    @PostMapping("/listByUserId")
    public TableDataInfo listByUserId(TrainListDto trainListDto)
    {
        startPage();
        List<TrainInfo> list = trainInfoService.listByUserId(trainListDto);
        return getDataTable(list);
    }
    /**
     * 导出培训信息列表
     */
  /*  @PreAuthorize(hasPermi = "business/train:trainInfo:export")
    @Log(title = "培训信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TrainInfo trainInfo) throws IOException
    {
        List<TrainInfo> list = trainInfoService.selectTrainInfoListToexport(trainInfo);
        BusinessExcelUtil<TrainInfo> util = new BusinessExcelUtil<TrainInfo>(TrainInfo.class);
        util.exportExcel(response, list, "培训信息数据");
    }
*/
    /**
     * 获取培训信息详细信息
     */
    @PreAuthorize(hasPermi = "business/train:trainInfo:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(trainInfoService.selectTrainInfoById(id));
    }

    /**
     * 新增培训信息
     */
    @PreAuthorize(hasPermi = "business/train:trainInfo:add")
    @Log(title = "培训信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add( @RequestBody TrainInfoDto trainInfoDto)
    {


        return toAjax(trainInfoService.insertTrainInfo(trainInfoDto));
    }

    /**
     * 修改培训信息
     */
    @PreAuthorize(hasPermi = "business/train:trainInfo:edit")
    @Log(title = "培训信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit( @RequestBody TrainInfoDto trainInfoDto)throws ServiceException
    {

        return toAjax(trainInfoService.updateTrainInfo(trainInfoDto));
    }

    /**
     * 删除培训信息
     */
    @PreAuthorize(hasPermi = "business/train:trainInfo:remove")
    @Log(title = "培训信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)throws ServiceException
    {
        return toAjax(trainInfoService.deleteTrainInfoByIds(ids));
    }

    /**
    * 开启培训
    */
     @PreAuthorize(hasPermi = "business/train:trainInfo:star")
     @GetMapping("/star/{ids}")
     public AjaxResult satr(@PathVariable String[] ids)
     { return toAjax(trainInfoService.starTrainInfoByIds(ids)); }


    /**
     * 用户培训结束后判断培训是否结束
     *  @param id 培训信息主键
     */
    @GetMapping(value = "/updateTrainInfoforUserTrainEnd")
    public void updateTrainInfoforUserTrainEnd(String id)
    {
        trainInfoService.updateTrainInfoforUserTrainEnd(id);
    }

    /*public static void main(String[] args) {
        char a='A';
        char b= (char) (a+1);
        System.out.println(b);
    }*/

}
